package red.lightblue.admin.service.impl;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import red.lightblue.admin.dao.ProductTypeDAO;
import red.lightblue.admin.dto.ProductTypeDTO;
import red.lightblue.admin.model.ProductType;
import red.lightblue.admin.qc.ProductTypeQC;
import red.lightblue.admin.service.ProductTypeService;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class ProductTypeServiceImpl implements ProductTypeService{

    @Autowired
    private ProductTypeDAO productTypeDAO;

    /**
     * 获取所有的ProductType
     * @return
     */
    @Override
    public List<ProductTypeDTO> getAllProductType() {
        List<ProductType> productTypes = productTypeDAO.listProductType(new ProductTypeQC());
        List<ProductTypeDTO> productTypeDTOS = productTypes.stream().map(type->
                {ProductTypeDTO dto = new ProductTypeDTO();BeanUtils.copyProperties(type,dto);
                return  dto;}).collect(Collectors.toList());
        return productTypeDTOS;
    }

    /**
     * 获取当前节点的子节点
     * @param id 当前节点的id
     * @return
     */
    @Override
    public List<ProductTypeDTO> getChildrenProductType(Integer id) {
        ProductTypeQC productTypeQC = new ProductTypeQC();
        productTypeQC.setProductId(id);
        List<ProductType> productTypes = productTypeDAO.listProductType(productTypeQC);
        List<ProductTypeDTO> productTypeDTOS = productTypes.stream().map(type->
        {ProductTypeDTO dto = new ProductTypeDTO();BeanUtils.copyProperties(type,dto);
            return  dto;}).collect(Collectors.toList());
        return productTypeDTOS;
    }
}